package com.insuremonitor.system.service.impl;

import com.insuremonitor.common.core.domain.AjaxResult;
import com.insuremonitor.common.utils.DateUtils;
import com.insuremonitor.system.domain.FrontInsureRateGoods;
import com.insuremonitor.system.domain.FrontInsureRateKind;
import com.insuremonitor.system.mapper.FrontInsureRateGoodsMapper;
import com.insuremonitor.system.mapper.FrontInsureRateKindMapper;
import com.insuremonitor.system.service.IFrontInsureRateKindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 货物类型与费率的关系Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-11-21
 */
@Service
public class FrontInsureRateKindServiceImpl implements IFrontInsureRateKindService 
{
    @Autowired
    private FrontInsureRateKindMapper frontInsureRateKindMapper;

    @Autowired
    private FrontInsureRateGoodsMapper frontInsureRateGoodsMapper;


    /**
     * 查询货物类型与费率的关系
     * 
     * @param insureKindId 货物类型与费率的关系主键
     * @return 货物类型与费率的关系
     */
    @Override
    public FrontInsureRateKind selectFrontInsureRateKindByInsureKindId(Long insureKindId)
    {
        return frontInsureRateKindMapper.selectFrontInsureRateKindByInsureKindId(insureKindId);
    }

    /**
     * 查询货物类型与费率的关系列表
     * 
     * @param frontInsureRateKind 货物类型与费率的关系
     * @return 货物类型与费率的关系
     */
    @Override
    public List<FrontInsureRateKind> selectFrontInsureRateKindList(FrontInsureRateKind frontInsureRateKind)
    {
        return frontInsureRateKindMapper.selectFrontInsureRateKindList(frontInsureRateKind);
    }

    /**
     * 新增货物类型与费率的关系
     * 
     * @param frontInsureRateKind 货物类型与费率的关系
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult insertFrontInsureRateKind(FrontInsureRateKind frontInsureRateKind)
    {
        frontInsureRateKind.setCreateTime(DateUtils.getNowDate());
        int result = frontInsureRateKindMapper.insertFrontInsureRateKind(frontInsureRateKind);
        if(result>0){
            List<FrontInsureRateGoods> insureRateGoods = frontInsureRateKind.getInsureRateGoodss();
            for(FrontInsureRateGoods insureRateGood  :insureRateGoods){
                insureRateGood.setInsureKindId(frontInsureRateKind.getInsureKindId());
                insureRateGood.setCreateTime(new Date());
                frontInsureRateGoodsMapper.insertFrontInsureRateGoods(insureRateGood);
            }
        }
        return AjaxResult.success("操作成功",frontInsureRateKind.getInsureKindId());
    }

    /**
     * 修改货物类型与费率的关系
     * 
     * @param frontInsureRateKind 货物类型与费率的关系
     * @return 结果
     */
    @Override
    public int updateFrontInsureRateKind(FrontInsureRateKind frontInsureRateKind)
    {
        frontInsureRateKind.setUpdateTime(DateUtils.getNowDate());
        return frontInsureRateKindMapper.updateFrontInsureRateKind(frontInsureRateKind);
    }

    /**
     * 批量删除货物类型与费率的关系
     * 
     * @param insureKindIds 需要删除的货物类型与费率的关系主键
     * @return 结果
     */
    @Override
    public int deleteFrontInsureRateKindByInsureKindIds(Long[] insureKindIds)
    {
        return frontInsureRateKindMapper.deleteFrontInsureRateKindByInsureKindIds(insureKindIds);
    }

    /**
     * 删除货物类型与费率的关系信息
     * 
     * @param insureKindId 货物类型与费率的关系主键
     * @return 结果
     */
    @Override
    public int deleteFrontInsureRateKindByInsureKindId(Long insureKindId)
    {
        return frontInsureRateKindMapper.deleteFrontInsureRateKindByInsureKindId(insureKindId);
    }
}
